<template>
  <div class="tenant-account-container">
    <!-- 搜索区域 -->
    <ContentWrap class="search-wrapper">
      <div class="search-header">
        <div class="header-icon">
          <Icon icon="ep:search" />
        </div>
        <div class="header-content">
          <h4 class="search-title">租户账户管理</h4>
          <p class="search-subtitle">查询和管理租户账户套餐信息</p>
        </div>
      </div>

      <div class="search-form-container">
        <el-form
          :model="queryParams"
          ref="queryFormRef"
          :inline="true"
          label-width="100px"
          label-position="left"
          class="enhanced-search-form"
        >
          <div class="form-row">
            <el-form-item label="租户名称" prop="tenantId" class="form-item">
              <el-select
                class="enhanced-select"
                v-model="queryParams.tenantId"
                filterable
                remote
                :remote-method="remoteMethod"
                clearable
                @clear="getList"
                placeholder="请选择租户名称"
                :loading="remoteLoading"
              >
                <template #prefix>
                  <Icon icon="ep:office-building" />
                </template>
                <el-option
                  v-for="item in tennatList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id!"
                >
                  <div class="option-item">
                    <Icon icon="ep:office-building" class="option-icon" />
                    <span>{{ item.name }}</span>
                  </div>
                </el-option>
              </el-select>
            </el-form-item>
          </div>
        </el-form>

        <div class="form-actions">
          <el-button @click="handleQuery" type="primary" class="action-btn search-btn">
            <Icon icon="ep:search" class="btn-icon" />
            搜索
          </el-button>
          <el-button @click="resetQuery" class="action-btn reset-btn">
            <Icon icon="ep:refresh" class="btn-icon" />
            重置
          </el-button>
        </div>
      </div>
    </ContentWrap>

    <!-- 列表区域 -->
    <ContentWrap class="table-wrapper">
      <div class="table-header">
        <div class="header-icon">
          <Icon icon="ep:list" />
        </div>
        <div class="header-content">
          <h4 class="table-title">账户列表</h4>
          <p class="table-subtitle">共 {{ total }} 个账户</p>
        </div>
      </div>

      <div class="table-container">
        <el-table
          :data="list"
          class="enhanced-table"
          :header-cell-style="{ background: '#f8f9ff', color: '#303133', fontWeight: '600' }"
        >
          <el-table-column label="套餐名称" prop="productName">
            <template #default="scope">
              <div class="product-cell">
                <Icon icon="ep:box" class="cell-icon" />
                <span class="cell-text">{{ scope.row.productName }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="用户" prop="ownerUsername">
            <template #default="scope">
              <div class="user-cell">
                <Icon icon="ep:user" class="cell-icon" />
                <span class="cell-text">{{ scope.row.ownerUsername }}</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="总额度" prop="totalNum">
            <template #default="scope">
              <div class="quota-cell total">
                <Icon icon="ep:pie-chart" class="cell-icon" />
                <span class="quota-text">{{ scope.row.totalNum }}</span>
                <span class="unit">分钟</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="剩余" prop="remainNum">
            <template #default="scope">
              <div class="quota-cell remain">
                <Icon icon="ep:clock" class="cell-icon" />
                <span class="quota-text">{{ scope.row.remainNum }}</span>
                <span class="unit">分钟</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="状态" prop="status">
            <template #default="scope">
              <div class="status-cell">
                <el-tag v-if="scope.row.status == 1" type="success" class="enhanced-tag">
                  <Icon icon="ep:check" class="tag-icon" />
                  使用中
                </el-tag>
                <el-tag v-if="scope.row.status == 2" type="warning" class="enhanced-tag">
                  <Icon icon="ep:warning" class="tag-icon" />
                  已用完
                </el-tag>
                <el-tag v-if="scope.row.status == 9" type="danger" class="enhanced-tag">
                  <Icon icon="ep:close" class="tag-icon" />
                  已过期
                </el-tag>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="有效期（天）" prop="expireTime">
            <template #default="scope">
              <div class="expire-cell">
                <Icon icon="ep:calendar" class="cell-icon" />
                <span v-if="scope.row.expireTime == 0" class="cell-text permanent">永久</span>
                <span v-else class="cell-text">{{ scope.row.expireTime }} 天</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="有效期至" prop="expireAt" :formatter="dateFormatter">
            <template #default="scope">
              <div class="date-cell">
                <Icon icon="ep:calendar" class="cell-icon" />
                <span class="cell-text">{{
                  dateFormatter(scope.row, null, scope.row.expireAt)
                }}</span>
              </div>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="pagination-wrapper">
          <Pagination
            :total="total"
            v-model:page="queryParams.pageNo"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
          />
        </div>
      </div>
    </ContentWrap>
  </div>
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import * as TenantAccountApi from '@/api/system/tenantAccount'
import * as TenantApi from '@/api/system/tenant'

defineOptions({ name: 'TenantAccount' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  tenantId: null
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    if (queryParams.tenantId) {
      const data = await TenantApi.getPackageListBySystem(queryParams)
      list.value = data.list
      total.value = data.total
    } else {
      list.value.length = 0
      total.value = 0
    }
  } finally {
    loading.value = false
  }
}

const tennatList = ref([])
const remoteLoading = ref(false)
const remoteMethod = async (query: string) => {
  remoteLoading.value = true
  const data = await TenantApi.getTenantPage({
    pageNo: 1,
    pageSize: 10,
    name: query
  })
  tennatList.value = data.list
  remoteLoading.value = false
  getList()
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 初始化 **/
onMounted(() => {
  remoteMethod()
})
</script>

<style lang="scss" scoped>
// 主题色变量
$primary-color: #0052cc;
$primary-light: rgba(0, 82, 204, 0.1);
$primary-border: rgba(0, 82, 204, 0.3);
$success-color: #67c23a;
$warning-color: #e6a23c;
$danger-color: #f56c6c;
$info-color: #909399;
$text-primary: #303133;
$text-regular: #606266;
$text-secondary: #909399;
$text-placeholder: #c0c4cc;
$border-color: #dcdfe6;
$border-light: #e4e7ed;
$bg-color: #f5f7fa;
$shadow-light: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
$shadow-base:
  0 2px 4px rgba(0, 0, 0, 0.12),
  0 0 6px rgba(0, 0, 0, 0.04);
$border-radius: 12px;
$border-radius-small: 8px;

.tenant-account-container {
  padding: 0;
  background: $bg-color;
  min-height: 100vh;

  @media (max-width: 768px) {
    padding: 0;
  }
}

/* 搜索区域样式 */
.search-wrapper {
  margin-bottom: 16px;
  border-radius: $border-radius;
  overflow: hidden;
  box-shadow: $shadow-light;
  border: 1px solid $border-light;

  :deep(.el-card__body) {
    padding: 0;
  }
}

.search-header {
  display: flex;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  color: white;

  @media (max-width: 768px) {
    padding: 16px 20px;
    flex-direction: column;
    gap: 8px;
    text-align: center;
  }
}

.header-icon {
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: $border-radius-small;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    width: 36px;
    height: 36px;
    font-size: 16px;
  }
}

.header-content {
  flex: 1;
  margin-left: 12px;

  @media (max-width: 768px) {
    margin-left: 0;
  }
}

.search-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 16px;
  }
}

.search-subtitle {
  font-size: 13px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

.search-form-container {
  padding: 24px;
  background: white;

  @media (max-width: 768px) {
    padding: 16px;
  }
}

.enhanced-search-form {
  .form-row {
    display: flex;
    flex-wrap: wrap;
    gap: 16px;
    margin-bottom: 20px;

    @media (max-width: 768px) {
      flex-direction: column;
      gap: 12px;
      margin-bottom: 16px;
    }
  }

  .form-item {
    flex: 1;
    min-width: 260px;
    margin-bottom: 0;

    @media (max-width: 768px) {
      min-width: auto;
      flex: none;
    }

    :deep(.el-form-item__label) {
      font-weight: 600;
      color: $text-regular;
      padding-bottom: 8px;

      @media (max-width: 768px) {
        font-size: 14px;
        width: 100% !important;
        text-align: left !important;
        margin-bottom: 8px;
      }
    }

    :deep(.el-form-item__content) {
      @media (max-width: 768px) {
        margin-left: 0 !important;
      }
    }
  }

  // H5适配：表单项垂直布局
  @media (max-width: 768px) {
    :deep(.el-form-item) {
      display: block;
    }
  }
}

/* 选择器样式 */
.enhanced-select {
  width: 100%;

  :deep(.el-select__wrapper) {
    border-radius: $border-radius-small;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;
    padding: 8px 16px;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focused {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
      border-color: $primary-color;
    }
  }
}

/* 选项样式 */
.option-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 0;
}

.option-icon {
  font-size: 14px;
  color: $primary-color;
}

/* 操作按钮区域 */
.form-actions {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;

  @media (max-width: 768px) {
    flex-direction: column;
    gap: 8px;
  }
}

.action-btn {
  border-radius: $border-radius-small;
  padding: 10px 16px;
  font-weight: 500;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;

  &:hover {
    transform: translateY(-1px);
  }

  @media (max-width: 768px) {
    width: 100%;
    justify-content: center;
    padding: 12px 16px;
  }

  .btn-icon {
    font-size: 14px;
  }

  &.search-btn {
    background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
    border: none;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.3);
    }
  }

  &.reset-btn {
    border: 2px solid $border-color;
    color: $text-regular;

    &:hover {
      border-color: $primary-color;
      color: $primary-color;
      background: $primary-light;
    }
  }
}

/* 表格区域样式 */
.table-wrapper {
  border-radius: $border-radius;
  overflow: hidden;
  box-shadow: $shadow-light;
  border: 1px solid $border-light;

  :deep(.el-card__body) {
    padding: 0;
  }
}

.table-header {
  display: flex;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  color: white;

  @media (max-width: 768px) {
    padding: 16px 20px;
    flex-direction: column;
    gap: 8px;
    text-align: center;
  }
}

.table-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 16px;
  }
}

.table-subtitle {
  font-size: 13px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

.table-container {
  background: white;
  overflow-x: auto;
}

.enhanced-table {
  :deep(.el-table__header) {
    th {
      background: linear-gradient(135deg, #f8f9ff 0%, #f0f2ff 100%) !important;
      color: $text-primary !important;
      font-weight: 600 !important;
      border-bottom: 2px solid $primary-light;
      padding: 16px 12px;
    }
  }

  :deep(.el-table__body) {
    tr {
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light !important;
      }

      td {
        padding: 14px 12px;
        border-bottom: 1px solid $border-light;
      }
    }

    .el-table__row--striped {
      background: #fafbff;

      &:hover {
        background: $primary-light !important;
      }
    }
  }

  // H5适配
  @media (max-width: 768px) {
    :deep(.el-table__header) th {
      padding: 12px 8px;
      font-size: 13px;
    }

    :deep(.el-table__body) td {
      padding: 12px 8px;
      font-size: 12px;
    }
  }
}

/* 表格单元格样式 */
.product-cell,
.user-cell,
.expire-cell,
.date-cell {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
}

.quota-cell {
  display: flex;
  align-items: center;
  gap: 6px;
  justify-content: center;

  &.total {
    .cell-icon {
      color: $primary-color;
    }
    .quota-text {
      color: $primary-color;
      font-weight: 600;
    }
  }

  &.remain {
    .cell-icon {
      color: $success-color;
    }
    .quota-text {
      color: $success-color;
      font-weight: 600;
    }
  }
}

.status-cell {
  display: flex;
  justify-content: center;
}

.cell-icon {
  font-size: 14px;
  flex-shrink: 0;
  color: $primary-color;
}

.cell-text {
  color: $text-regular;
  font-weight: 500;

  &.permanent {
    color: $success-color;
    font-weight: 600;
  }

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

.quota-text {
  font-size: 16px;
  font-weight: 600;

  @media (max-width: 768px) {
    font-size: 14px;
  }
}

.unit {
  font-size: 11px;
  color: $text-secondary;
  background: rgba(0, 0, 0, 0.05);
  padding: 1px 4px;
  border-radius: 3px;

  @media (max-width: 768px) {
    font-size: 10px;
  }
}

/* 标签样式 */
.enhanced-tag {
  border-radius: 6px;
  font-weight: 500;
  padding: 4px 8px;
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 4px;

  @media (max-width: 768px) {
    font-size: 11px;
    padding: 3px 6px;
  }
}

.tag-icon {
  font-size: 12px;
}

/* 分页样式 */
.pagination-wrapper {
  display: flex;
  justify-content: center;
  padding: 20px 24px;
  background: white;
  border-top: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px 20px;
  }

  :deep(.el-pagination) {
    .el-pager li {
      border-radius: 6px;
      margin: 0 2px;
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light;
        color: $primary-color;
      }

      &.is-active {
        background: $primary-color;
        color: white;
      }
    }

    .btn-prev,
    .btn-next {
      border-radius: 6px;
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light;
        color: $primary-color;
      }
    }

    @media (max-width: 768px) {
      .el-pagination__sizes,
      .el-pagination__jump {
        display: none;
      }
    }
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .tenant-account-container {
    background: #1f2937;
  }

  .search-wrapper,
  .table-wrapper {
    background: #374151;
    border-color: #4b5563;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  }

  .search-form-container,
  .table-container,
  .pagination-wrapper {
    background: #374151;
  }

  .search-title,
  .table-title {
    color: #f9fafb;
  }

  .cell-text {
    color: #f9fafb;

    &.permanent {
      color: #34d399;
    }
  }

  .enhanced-table {
    :deep(.el-table__header) {
      th {
        background: #4b5563 !important;
        color: #f9fafb !important;
        border-bottom-color: #6b7280;
      }
    }

    :deep(.el-table__cell) {
      border-bottom-color: #4b5563;
    }
  }

  .enhanced-select {
    :deep(.el-select__wrapper) {
      background: #4b5563;
      border-color: #6b7280;

      .el-input__inner {
        color: #f9fafb;
      }
    }
  }

  .option-icon {
    color: #60a5fa;
  }

  .unit {
    background: rgba(255, 255, 255, 0.1);
    color: #9ca3af;
  }

  .quota-cell {
    &.total {
      .cell-icon {
        color: #60a5fa;
      }
      .quota-text {
        color: #60a5fa;
      }
    }

    &.remain {
      .cell-icon {
        color: #34d399;
      }
      .quota-text {
        color: #34d399;
      }
    }
  }
}

/* 响应式优化 */
@media (max-width: 480px) {
  .search-header,
  .table-header {
    padding: 12px 16px;
  }

  .search-form-container {
    padding: 12px;
  }

  .pagination-wrapper {
    padding: 12px 16px;
  }

  .enhanced-table {
    :deep(.el-table__header) th {
      padding: 10px 6px;
      font-size: 12px;
    }

    :deep(.el-table__body) td {
      padding: 10px 6px;
      font-size: 11px;
    }
  }

  .cell-text {
    font-size: 11px;
  }

  .quota-text {
    font-size: 13px;
  }

  .unit {
    font-size: 9px;
  }

  .enhanced-tag {
    font-size: 10px;
    padding: 2px 4px;
  }
}
</style>
